﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using ReportWizard.DataAccess;
using ReportWizard.Forms;
using ReportWizard.Helpers;
using System.Text;

namespace ReportWizard.Controls
{
    public partial class TemplatePhotoSelect : UserControl
    {
        #region properties

        public WordBookmark CurrentBookmark { get; set; }

        public string FieldTitle
        {
            get { return lFieldName.Text.Trim(); }
            set { lFieldName.Text = value; }
        }

        public string FieldValue
        {
            get { return tbPhotoFolderPath.Text.Trim(); }
            set { tbPhotoFolderPath.Text = value; }
        }

        #endregion

        public TemplatePhotoSelect(WordBookmark wb)
        {
            CurrentBookmark = wb;

            InitializeComponent();

            FillBookmarkInfo(wb);
        }

        private void FillBookmarkInfo(WordBookmark wb)
        {
            FieldTitle = CurrentBookmark.Name.Replace('_', ' ');

            if (!string.IsNullOrEmpty(wb.Description))
            {
                pbInfo.Visible = true;
                ttBookmark.SetToolTip(pbInfo, wb.Description);
            }
        }

        private void bPhotoAddToSet_Click(object sender, EventArgs e)
        {
            Button openButton = (Button) sender;
            if (openButton != null)
            {
                Point openButtonLocation = openButton.PointToClient(Cursor.Position);
                cmsOpenPhotos.Show(openButton, openButtonLocation.X, openButtonLocation.Y);
            }
        }

        private void SetToolTipMessage()
        {
            if (Globals.CurrentReport.PhotoCollection != null)
            {
                tbPhotoFolderPath.Clear();

                var bookmarkPhotoCollection =
                    Globals.CurrentReport.PhotoCollection.Where(i => i.BookmarkId == CurrentBookmark.Id).FirstOrDefault();
                if (bookmarkPhotoCollection != null)
                {
                    StringBuilder sb = new StringBuilder();

                    int fileIndex = 0;
                    IEnumerable<string> fileNames = bookmarkPhotoCollection.Photos.Where(k => k.Checked).Select(j => j.FileName);
                    foreach (string fileName in fileNames)
                    {
                        fileIndex++;
                        sb.AppendFormat("{0} - {1}{2}", fileIndex, fileName, Environment.NewLine);
                    }

                    tbPhotoFolderPath.Text = string.Format("{0} фото", fileNames.Count().ToString());
                    ttBookmark.SetToolTip(tbPhotoFolderPath, sb.ToString());
                }
            }
        }

        private void miAddFile_Click(object sender, EventArgs e)
        {
            if (dlgOpenPhoto.ShowDialog() == DialogResult.OK)
            {
                if (dlgOpenPhoto.FileNames.Length != 0)
                {
                    Globals.TargetFormProfile = CurrentBookmark.Name;

                    SelectImagesForm selectImagesForm = new SelectImagesForm(CurrentBookmark.Id, dlgOpenPhoto.FileNames);
                    selectImagesForm.FillFileListFromFileNames();
                    SetToolTipMessage();
                }
            }
        }

        private void miAddFolder_Click(object sender, EventArgs e)
        {
            if (dlgOpenPhotoFolder.ShowDialog() == DialogResult.OK)
            {
                if (Directory.Exists(dlgOpenPhotoFolder.SelectedPath))
                {
                    Globals.TargetFormProfile = CurrentBookmark.Name;

                    SelectImagesForm selectImagesForm = new SelectImagesForm(CurrentBookmark.Id, dlgOpenPhotoFolder.SelectedPath);
                    selectImagesForm.FillFileListFromFolder();
                    SetToolTipMessage();
                }
            }
        }

        private void bPhotoEditSet_Click(object sender, EventArgs e)
        {
            Globals.TargetFormProfile = CurrentBookmark.Name;

            SelectImagesForm selectImagesForm = new SelectImagesForm(CurrentBookmark.Id);
            selectImagesForm.FillFileListFromGlobal();
            SetToolTipMessage();
        }
    }
}
